﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CinemaBookingWebsite
{
    public partial class Booking : System.Web.UI.Page
    {

        //CinemaWcf.ICinemaWcf cinemaWcf = new CinemaWcf.CinemaWcfClient();
        CinemaWcf.ICinemaWcf cinemaWcf = new CinemaWcf.CinemaWcfClient();
        BookingWcf.IBookingSeats bookingWcf = new BookingWcf.BookingSeatsClient();
        int auditoriumID;
        int screeningID;
        int selectedRow;

        protected void Page_Load(object sender, EventArgs e)
        {
            string strId = Request.QueryString["ScreeningID"];
            int id;
            if (int.TryParse(strId, out id))
            {
                auditoriumID = cinemaWcf.GetAuditoriumByScreeningID(id);
                screeningID = id;
                if (!IsPostBack)
                {



                    //CinemaWcf.ICinemaWcf cinemaWcf = new CinemaWcf.CinemaWcfClient();

                    //var a = cinemaWcf.GetAuditoriumByScreeningID(id);
                    //var s = cinemaWcf


                    RepeaterAud.DataSource = cinemaWcf.GetRowsByAuditoriumID(auditoriumID);
                    RepeaterAud.DataBind();

                    DDLRow.DataValueField = ("RowsID");
                    DDLRow.DataTextField = ("RowNumber");
                    DDLRow.DataSource = cinemaWcf.GetRowsByAuditoriumID(auditoriumID);
                    DDLRow.DataBind();
                    DDLRow.Items.Insert(0, new ListItem(string.Empty, string.Empty));
                    //DDLRow.SelectedIndex = 0;



                }
            }


        }
        protected void ItemBound(object sender, RepeaterItemEventArgs args)
        {
            if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater RepeaterRow = (Repeater)args.Item.FindControl("RepeaterRow");
                RepeaterRow.DataSource = cinemaWcf.GetRowSeats(auditoriumID);
                RepeaterRow.DataBind();
            }
        }

        protected void DDLRow_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedRow = Convert.ToInt32(DDLRow.SelectedValue);
            //selectedRow = DDLRow.SelectedValue;
            DDLChoice.DataValueField = ("SeatID");
            DDLChoice.DataTextField = ("SeatNumber");
            //DDLChoice.DataSource = cinemaWcf.GetAllSeatsNotBookedByRowID(screeningID, selectedRow);
            DDLChoice.DataSource = cinemaWcf.GetAllSeatsNotBookedByRowID(screeningID, selectedRow);
            DDLChoice.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int number = Convert.ToInt32(DDLNumber.SelectedValue);
            int choice = Convert.ToInt32(DDLChoice.SelectedValue);
            string name = txtName.Text;
            int phone = Convert.ToInt32(txtPhone.Text);

            bool orderCreated = bookingWcf.CreateBooking(number, choice, screeningID, name, phone);

            if(orderCreated == true)
            {
                Response.Redirect("Result.aspx?Created=true");
            }
            if(orderCreated == false)
            {
                Response.Redirect("Result.aspx?Created=false");
            }

        }

    }
}